package com.egdigital.allzic.utils;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.egdigital.allzic.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ImageManager {

	private static final String	TAG	= ImageManager.class.getSimpleName();	// log
	private static ImageManager	mInstance;									// singleton
	private Context				mContext;									// application context
	//
	private ImageLoader			mImageLoader;
	private DisplayImageOptions	mOptionsSimple;

	public ImageManager(Context context) {
		mContext = context;
		mImageLoader = ImageLoader.getInstance();
	}

	public static ImageManager getInstance(Context context) {
		if (mInstance == null) {
			synchronized (ImageManager.class) {
				if (mInstance == null) {
					mInstance = new ImageManager(context);
				}
			}
		}
		return mInstance;
	}

	public void setOptions() {
		this.mOptionsSimple = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
																.showImageForEmptyUri(R.drawable.cd_bgdark)
																.showImageOnLoading(R.drawable.cd_bgdark)
																.showImageOnFail(R.drawable.cd_bgdark)
																.cacheOnDisc(true)
																.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
																.bitmapConfig(Bitmap.Config.RGB_565)
																.build();
	}

	public void setOptionsTuto() {
		this.mOptionsSimple = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
																.showImageForEmptyUri(R.drawable.img_empty)
																.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
																.bitmapConfig(Bitmap.Config.RGB_565)
																.build();
	}

	public void setImageLoader() {
		File cacheDir = StorageUtils.getOwnCacheDirectory(mContext, mContext.getString(R.string.app_name) + "/cache");
		ImageLoaderConfiguration config = null;
		config = new ImageLoaderConfiguration.Builder(mContext).threadPoolSize(3)
																.threadPriority(Thread.NORM_PRIORITY - 1)
																.tasksProcessingOrder(QueueProcessingType.FIFO)
																.imageDownloader(new MyImageDownloader(mContext))
																.imageDecoder(new BaseImageDecoder(false))
																.discCache(new UnlimitedDiscCache(cacheDir))
																.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
																.defaultDisplayImageOptions(mOptionsSimple)
																.writeDebugLogs()
																.build();
		if (!mImageLoader.isInited()) {
			mImageLoader.init(config);
		}
	}

	/**
	 * @param imageWidth
	 * @param imageHeight
	 */
	public void setImageLoader(int imageWidth, int imageHeight) {
		File cacheDir = StorageUtils.getCacheDirectory(mContext);
		ImageLoaderConfiguration config = null;
		config = new ImageLoaderConfiguration.Builder(mContext).discCacheExtraOptions(imageWidth, imageHeight, CompressFormat.PNG, 75, null)
																.threadPoolSize(3)
																.threadPriority(Thread.NORM_PRIORITY - 2)
																.tasksProcessingOrder(QueueProcessingType.FIFO)
																.denyCacheImageMultipleSizesInMemory()
																.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
																.memoryCacheExtraOptions(imageWidth, imageHeight)
																.discCache(new UnlimitedDiscCache(cacheDir))
																.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
																.imageDownloader(new MyImageDownloader(mContext))
																.defaultDisplayImageOptions(mOptionsSimple)
																.build();
		if (!mImageLoader.isInited()) {
			mImageLoader.init(config);
		}
	}

	public DisplayImageOptions getmSimpleOptions() {
		return mOptionsSimple;
	}

	public ImageLoader getmImageLoader() {
		return mImageLoader;
	}

	public class MyImageDownloader extends BaseImageDownloader {

		public MyImageDownloader(Context context) {
			super(context);
		}

		@Override
		protected HttpURLConnection createConnection(String url, Object extra) throws IOException {
			HttpURLConnection conn = super.createConnection(url, extra);
			conn.setRequestProperty("Content-Length", "0");
			return conn;
		}
	}
}
